package tsli.acm.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import tsli.acm.dao.GAAgentStructureDAO;
import tsli.acm.database.Database;

/**
 * Servlet implementation class ACM1102Srvl
 */

public class ACM1102Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ACM1102Srvl() {
        super();
    }
    
    public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/JSON");
		Database db = new Database();
    	String vaPeNo = request.getParameter("peNo");
    	String vaMonth = request.getParameter("month");
		String vaYear = request.getParameter("year");
		String vaYearMonth = vaYear+vaMonth;
    	GAAgentStructureDAO voGaAgentStructureDAO = new GAAgentStructureDAO(db);
    	List<Map<String, Object>> voResult = voGaAgentStructureDAO.showDetails(vaYearMonth, vaPeNo) ;
    	String[] vaPeNO = null, vaName = null , vaDuty = null, vaPc=null, vaPcNum = null;
		vaPeNO = new String[voResult.size()]; 
		vaName = new String[voResult.size()];
		vaDuty = new String[voResult.size()];
		vaPcNum = new String[voResult.size()];
		vaPc = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaPeNO[i] = (String) voObject.get("PE_NO"); 
			vaName[i] = (String) voObject.get("NM_TITLE_THAI")+voObject.get("NM_THAI")+"&nbsp;&nbsp;&nbsp;&nbsp;"+voObject.get("SURNM_THAI");	
			vaDuty[i] = (String) voObject.get("DTY_CD");
			vaPcNum[i] = (String)voObject.get("PC").toString();
			vaPc[i]   = formatMoney( voObject.get("PC").toString());
		}
		
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaPeNO != null) {
			for (int i = 0; i < vaPeNO.length; i ++) {
				voList.add(vaPeNO[i]);
			}
		}
		voObject.put("PeNo", voList);
		
		JSONArray voList2 = new JSONArray();
		if (vaName != null) {
			for (int i = 0; i < vaName.length; i ++) {
				voList2.add(vaName[i]);
			}
		}
		voObject.put("name",voList2);
		
		
		JSONArray voList3 = new JSONArray();
		if (vaDuty != null) {
			for (int i = 0; i < vaDuty.length; i ++) {
				voList3.add(vaDuty[i]);
			}
		}
		voObject.put("duty",voList3);
		
		JSONArray voList4 = new JSONArray();
		double sum = 0.0;
		if (vaPc != null) {
			for (int i = 0; i < vaPcNum.length; i ++) {
				voList4.add(vaPc[i]);
				sum += Double.parseDouble(vaPcNum[i]);
				
			}
		}
		voObject.put("pc",voList4);
		voObject.put("total",formatMoney(Double.toString(sum)));
		voOut.print(voObject.toJSONString());
		db.close();	

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//processRequest(request,response);
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/JSON");
    	Database db = new Database();
    	String vaPeNo = request.getParameter("peNo");
		String vaMonth = request.getParameter("month");
		String vaYear = request.getParameter("year");
		String vaYearMonth = vaYear+vaMonth;
		String vaProcessType = request.getParameter("process_type");
		ACM1102Srvl a = new ACM1102Srvl();
		if(vaPeNo==""){
			GAAgentStructureDAO voGaAgentStructureDAO = new GAAgentStructureDAO(db);
			List<Map<String, Object>> voResult = voGaAgentStructureDAO.showDataByMonthYear(vaYearMonth);
			String[] vaPeNO = null, vaName = null , vaNet = null;
			vaPeNO = new String[voResult.size()]; 
			vaName = new String[voResult.size()];
			vaNet = new String[voResult.size()];
			
			for (int i = 0; i < voResult.size(); i++) {
				Map<String, Object> voObject = voResult.get(i);	
				vaPeNO[i] = (String) voObject.get("PE_NO_SM"); 
				vaName[i] = (String) voObject.get("NM_TITLE_THAI")+voObject.get("NM_THAI")+"&nbsp;&nbsp;&nbsp;&nbsp;"+voObject.get("SURNM_THAI");	
				vaNet[i] = a.formatMoney(voObject.get("PC_AL").toString());
			}
			
			JSONObject voObject = new JSONObject();
			JSONArray voList = new JSONArray();
			PrintWriter voOut = response.getWriter();
			if (vaPeNO != null) {
				for (int i = 0; i < vaPeNO.length; i ++) {
					voList.add(vaPeNO[i]);
				}
			}
			voObject.put("PeNo", voList);
			
			JSONArray voList2 = new JSONArray();
			if (vaName != null) {
				for (int i = 0; i < vaName.length; i ++) {
					voList2.add(vaName[i]);
				}
			}
			voObject.put("name",voList2);
			
			
			JSONArray voList3 = new JSONArray();
			if (vaNet != null) {
				for (int i = 0; i < vaNet.length; i ++) {
					voList3.add(vaNet[i]);
				}
			}
			voObject.put("net",voList3);
			voOut.print(voObject.toJSONString());
			db.close();	
		}else{
			GAAgentStructureDAO voGaAgentStructureDAO = new GAAgentStructureDAO(db);
			List<Map<String, Object>> voResult = voGaAgentStructureDAO.showDataByMonthYearPeNo(vaYearMonth,vaPeNo);
			String[] vaPeNO = null, vaName = null , vaNet = null;
			vaPeNO = new String[voResult.size()]; 
			vaName = new String[voResult.size()];
			vaNet = new String[voResult.size()];
			for (int i = 0; i < voResult.size(); i++) {
				Map<String, Object> voObject = voResult.get(i);	
				vaPeNO[i] = (String) voObject.get("PE_NO_SM"); 
				vaName[i] = (String) voObject.get("NM_TITLE_THAI")+voObject.get("NM_THAI")+"&nbsp;&nbsp;&nbsp;&nbsp;"+voObject.get("SURNM_THAI");	
				vaNet[i] = a.formatMoney(voObject.get("PC_AL").toString());
			}
			
			JSONObject voObject = new JSONObject();
			JSONArray voList = new JSONArray();
			PrintWriter voOut = response.getWriter();
			if (vaPeNO != null) {
				for (int i = 0; i < vaPeNO.length; i ++) {
					voList.add(vaPeNO[i]);
				}
			}
			voObject.put("PeNo", voList);
			
			JSONArray voList2 = new JSONArray();
			if (vaName != null) {
				for (int i = 0; i < vaName.length; i ++) {
					voList2.add(vaName[i]);
				}
			}
			voObject.put("name",voList2);
			
			
			JSONArray voList3 = new JSONArray();
			if (vaNet != null) {
				for (int i = 0; i < vaNet.length; i ++) {
					voList3.add(vaNet[i]);
				}
			}
			voObject.put("net",voList3);
			voOut.print(voObject.toJSONString());
			db.close();	
			
		}
			
	}
	public String formatMoney(String money){
		double formatDouble = 0;
		NumberFormat formatter = new DecimalFormat("###,###,##0.00"); 
		try{
			formatDouble = Double.parseDouble(money);
		}catch(Exception e){
			System.out.println(e);
			return "0.00";
		}
		return formatter.format(formatDouble);
	}
	

}
